package com.concerneyes.health.Task;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.health.EditOptometryData;
import com.concerneyes.health.model.HealthDataModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import common.AbsActivity;
import common.PreferUtil;
import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;

/**
 * Created by Administrator on 2017/7/3 0003.
 */

public class GlassManDataImpl {
    private Context context;
    private GlassManDataListener listener;
    private AbsActivity absActivity;
    private HealthDataModel model;

    public GlassManDataImpl(AbsActivity absActivity, GlassManDataListener listener) {
        this.absActivity = absActivity;
        this.context = CEApp.getInstance();
        this.listener = listener;
    }

    public void setData(HealthDataModel model) {
        this.model = model;
    }

    /**
     * 配戴人数据列表
     */
    public void listGlassManData(String manId) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", String.valueOf(PreferUtil.getUserId()));
        params.put("manId", manId);
        HttpAsyncTask task = new HttpAsyncTask() {
            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                Log.e(this.getClass().getSimpleName(), "arg1==>" + arg1);
                absActivity.cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                List<HealthDataModel> model = null;
                if (TextUtils.isEmpty(arg1)) {
                    absActivity.showToast(R.string.network_error_tip);
                    return;
                }
                try {
                    if (ret.getInt("code") == 200) {
                        JSONObject object = new JSONObject(arg1);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, new TypeToken<ArrayList<HealthDataModel>>() {
                        }.getType());
                    } else {
                        absActivity.showToast(ret.getString("msg"));
                        listener.glassManDataListFail();
                    }
                } catch (Exception e) {
                }
                if (model != null && model.size() > 0) {
                    listener.glassManDataListSuccess(model);
                } else {
                    listener.glassManDataListFail();
                }
            }
        };
        task.setDebug(true);
        task.excute(Url.BaseUrl + "/eyeData/list", params, "POST");
    }

    /**
     * 删除配戴人数据
     *
     * @param id 对应配戴人的ID
     */
    public void deteleGlassManData(int id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("id", String.valueOf(id));
        params.put("uid", PreferUtil.getUserId());
        HttpAsyncTask task = new HttpAsyncTask() {

            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(arg1);
                List<HealthDataModel> model = null;
                try {
                    if (ret.getInt("code") == 200) {
                        JSONObject object = new JSONObject(arg1);
                        String str = object.getString("data");
                        model = new Gson().fromJson(str, new TypeToken<ArrayList<HealthDataModel>>() {
                        }.getType());
                    } else {
                        absActivity.showToast(ret.getString("msg"));
                        listener.deleteGlassManDataFail();
                    }
                } catch (Exception e) {
                }
                if (model != null && model.size() > 0) {
                    listener.deleteGlassManDataSuccess(model);
                } else {
                    listener.deleteGlassManDataFail();
                }
            }
        };
        task.setDebug(true);
        task.excute(Url.BaseUrl + "/eyeData/delete", params, "POST");
    }

    /**
     * 增加或修改配戴人数据
     */
    public void addGlassManData(boolean isAdd, String manId, String id) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", String.valueOf(PreferUtil.getUserId()));
        params.put("manId", manId);
        params.put("farLeftDegree", model.getFarLeftDegree());
        params.put("farLeftAstigmia", model.getFarLeftAstigmia());
        params.put("farLeftPupillary", model.getFarLeftPupillary());
        if (model.getFarLeftCorrect() != null) {
            params.put("farLeftCorrect", model.getFarLeftCorrect());
        } else {
            params.put("farLeftCorrect", String.valueOf(1));
        }
        params.put("farLeftAxialView", model.getFarLeftAxialView());
        params.put("farRightDegree", model.getFarRightDegree());
        params.put("farRightAstigmia", model.getFarRightAstigmia());
        params.put("farRightPupillary", model.getFarRightPupillary());
        if (model.getFarRightCorrect() != null) {
            params.put("farRightCorrect", model.getFarRightCorrect());
        } else {
            params.put("farRightCorrect", String.valueOf(1));
        }
        params.put("farRightAxialView", model.getFarRightAstigmia());

        if (model.getNearLeftDegree() != null) {
            params.put("nearLeftDegree", model.getNearLeftDegree());
        }
        if (model.getNearLeftAstigmia() != null) {
            params.put("nearLeftAstigmia", model.getNearLeftAstigmia());
        }
        if (model.getNearLeftPupillary() != null) {
            params.put("nearLeftPupillary", model.getNearLeftPupillary());
        }
        if (model.getNearLeftCorrect() != null) {
            params.put("nearLeftCorrect", model.getNearLeftCorrect());
        }
        if (model.getNearLeftAxialView() != null) {
            params.put("nearLeftAxialView", model.getNearLeftAxialView());
        }
        if (model.getNearRightDegree() != null) {
            params.put("nearRightDegree", model.getNearRightDegree());
        }
        if (model.getNearRightAstigmia() != null) {
            params.put("nearRightAstigmia", model.getNearRightAstigmia());
        }
        if (model.getNearRightPupillary() != null) {
            params.put("nearRightPupillary", model.getNearRightPupillary());
        }
        if (model.getNearRightCorrect() != null) {
            params.put("nearRightCorrect", model.getNearRightCorrect());
        }
        if (model.getNearRightAxialView() != null) {
            params.put("nearRightAxialView", model.getNearRightAxialView());
        }
        params.put("isDoublePupillary", String.valueOf(model.getIsDoublePupillary()));
        params.put("frameSize", model.getFrameSize());
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        params.put("dataTime", model.getDataTime());
        params.put("from", "康爱斯");
        if (!isAdd && id != null) {
            params.put("id", id);
        }
        Log.e("params", "" + params);
        HttpAsyncTask task = new HttpAsyncTask() {
            @Override
            public void onStart(String arg0) {
                absActivity.showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String arg0, String arg1) {
                absActivity.cancelLoadingDialog();
                Log.e("arg1", "" + arg1);
                JsonMap ret = JsonMap.parseJson(arg1);
                if (TextUtils.isEmpty(arg1)) {
                    listener.addGlassManDataFail();
                    return;
                }
                try {
                    if (ret.getInt("code") != 200) {
                        absActivity.showToast(ret.getString("msg"));
                        listener.addGlassManDataFail();
                    } else {
                        absActivity.showToast(ret.getString("msg"));
                        listener.addGlassManDataSuccess();
                    }
                } catch (Exception e) {
                    listener.addGlassManDataFail();
                }
            }
        };
        task.setDebug(true);
        if (isAdd) {
            task.excute(Url.BaseUrl + "/eyeData/add", params, "POST");
        } else {
            task.excute(Url.BaseUrl + "/eyeData/update", params, "POST");
        }

    }
}
